﻿// Example header text. Can be configured in the options.
using System;
using System.Data;
using System.Windows.Forms;
using Telerik.WinControls;
using vtech_formBase;
using vtech_frameWorks.Exceptions;
using vtech_frameWorks.Library;

namespace vtech_system.User
{
    public partial class frmUser : frmList
    {
        public frmUser()
        {
            this.InitializeComponent();
        }

        #region LoadData
        protected override void LoadData()
        {
            ClsUserBS obj = new ClsUserBS();
            this.dsUser.DtUser.Clear();
            this.dsUser.DtUser.Merge(obj.GetAll());

            this.dsUser.DtUser.RowChanging += new DataRowChangeEventHandler(DtUser_RowChanging);
        }

        void DtUser_RowChanging(object sender, DataRowChangeEventArgs e)
        {
            this.lblFullName.Text = string.Format("{0}{1}", this.lblFullName.Text, e.Row["FullName"].ToString());
            this.lblEmail.Text = lblEmail.Text + e.Row["Email"].ToString();
            this.lblHomeAddress.Text = string.Format("{0}{1}", this.lblHomeAddress.Text, e.Row["HomeAddress"].ToString());
            this.lblWorkAddress.Text = string.Format("{0}{1}", this.lblWorkAddress.Text, e.Row["WorkAddress"].ToString());
            this.lblMobilePhone.Text = string.Format("{0}{1}", this.lblMobilePhone.Text, e.Row["MobilePhone"].ToString());
            this.lblWebsite.Text = string.Format("{0}{1}", this.lblWebsite.Text, e.Row["Website"].ToString());
            this.lblHomePhone.Text = string.Format("{0}{1}", this.lblHomePhone.Text, e.Row["Website"].ToString());
            this.lblWorkPhone.Text = string.Format("{0}{1}", this.lblWorkPhone.Text, e.Row["WorkPhone"].ToString());
            this.lblFax.Text = string.Format("{0}{1}", this.lblFax.Text, e.Row["Fax"].ToString());
            pbAvatar.Image = ClsImages.ByteArrayToImage((byte[])e.Row["Photo"]);
        }

        private void frmUser_Load(object sender, System.EventArgs e)
        {
            base.frmList_Load(sender, e);
        }

        #endregion

        #region Add
        protected override void Add()
        {
            using (frmAddUser fAdd = new frmAddUser())
            {
                fAdd.OnAdd += LoadData;
                fAdd.ShowDialog();
            }
        }
        #endregion

        #region Edit
        protected override void Edit()
        {
            try
            {
                this.ID = (Guid)this.dgvUser.SelectedRows[0].Cells["ID"].Value;
                using (frmEditUser fEdit = new frmEditUser(this.ID))
                {
                    fEdit.OnEdit += LoadData;
                    fEdit.ShowDialog();
                }
            }
            catch (Exception ex)
            {
                ClsInformation.Error(ex.Message);
            }
        }

        #endregion
        
        private void gridUser_DoubleClick(object sender, EventArgs e)
        {
            this.Edit();
        }

        #region Delete
        protected override void Delete()
        {
            try
            {
                ClsUserBS obj = new ClsUserBS();
                if (this.dgvUser.SelectedRows.Count != 0)
                {
                    this.ID = new Guid(this.dgvUser.SelectedRows[0].Cells[0].Value.ToString());
                    if (DialogResult.Yes == RadMessageBox.Show("Bạn muốn người dùng này ?", "Thông báo", MessageBoxButtons.YesNo, RadMessageIcon.Question))
                    {
                        DataRow[] drow;
                        drow = obj.GetAll().Select(String.Format("ID = '{0}'", this.ID));
                        foreach (DataRow row in drow)
                        {
                            obj.DoDelete(row);
                        }
                        this.LoadData();
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    ClsInformation.Error("Chưa chọn bản ghi.Không thể xóa!");
                    return;
                }
            }
            catch (Exception ex)
            {
                ClsInformation.Error(ex.ToString());
            }
        }
        #endregion
    }
}